﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using NPOI;
using System.Threading;
using ImagesNameConvert.Helper;
using System.Configuration;

namespace ImagesNameConvert
{
    public partial class Form1 : Form
    {
        //允许的图片格式，读取时只读取这些格式的图片文件
        private readonly string[] AllowFileType = new string[] { ".jpg", ".png", ".jpeg", ".gif", ".bmp" };

        //每个目录要处理的最大图片数量
        private readonly int MaxCount = 5;

        //要处理的目录地址
        private string FolderPath;

        private delegate void SetTextBox2Handler(string FolderName);

        private void SetTextBox2(string Result)
        {
            if (textBox2.InvokeRequired == true)
            {
                SetTextBox2Handler set = new SetTextBox2Handler(SetTextBox2);
                textBox2.Invoke(set, new object[] { Result });
            }
            else
            {
                textBox2.Text += Result + "\r\n";
            }
            
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                if (this.folderBrowserDialog1.SelectedPath.Trim() != "")
                    textBox1.Text = this.folderBrowserDialog1.SelectedPath.Trim();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderPath = textBox1.Text;
            if(string.IsNullOrEmpty(FolderPath))
            {
                MessageBox.Show("请选择图片目录");
                return;
            }
            Thread t = new Thread(new ThreadStart(Run));
            t.Start();
        }


        private void Run()
        {
            SetTextBox2Handler handler = new SetTextBox2Handler(SetTextBox2);

            DataTable table = new DataTable();
        //    table.Columns.Add(new DataColumn() { ColumnName = "文件夹名称", DataType = typeof(string) });
			table.Columns.Add(new DataColumn() { ColumnName = "图片1", DataType = typeof(string) });
			table.Columns.Add(new DataColumn() { ColumnName = "图片2", DataType = typeof(string) }); 

			#region 读取下一层目录，只读取一层
			var List = Directory.GetDirectories(FolderPath);
            foreach (var item in List)
            {
                //读取目录下图片文件
                var FileList = Directory.GetFiles(item).Where(m => AllowFileType.Contains(Path.GetExtension(m).ToLower())).ToList();
                if (FileList.Count == 0)
                    continue;

                //最多只处理设定的图片数量，超出部分不处理
                int CurrentCount = FileList.Count;
                if (CurrentCount > MaxCount)
                    CurrentCount = MaxCount;

                //开始处理图片
                DataRow row = table.NewRow();
                string FolderName = new DirectoryInfo(item).Name;
              //  row["文件夹名称"] = FolderName;
				string filename = "";
				for (int i = 0; i < CurrentCount; i++)
				{
					if (i == 0)
					{
						row["图片" + (i + 1)] = ConfigurationManager.AppSettings["PreDomain"].TrimEnd('/') + "/" + FolderName + "/" + Path.GetFileName(FileList[i]);
					}
					else if (i == CurrentCount-1)
					{

						filename += ConfigurationManager.AppSettings["PreDomain"].TrimEnd('/') + "/" + FolderName + "/" + Path.GetFileName(FileList[i]) +";";

						filename = filename.Substring(0, filename.Length - 1);

						row["图片2"] = filename;
					}
					else {
						filename += ConfigurationManager.AppSettings["PreDomain"].TrimEnd('/') + "/" + FolderName + "/" + Path.GetFileName(FileList[i]) +";";
					}
				}
                table.Rows.Add(row);

                //写入textbox文本框
                handler("目录   " + item + "   处理成功");
            }

            handler("正在写入Excel");

            var Excel = new ExcelHelper("处理结果.xlsx");
            Excel.DataTableToExcel(table, "sheet1", true);

            //处理完成
            handler("处理完成");
            #endregion
        }
    }
}
